<?
class Util {
	public $hostName = "localhost";
	public $userName = "root";
	public $passwrd = "";
	public $dbname = "carservice";
	public $rootPath = "";
	public $perPage = 10;
	public $startLimit = 0;


	public function Util() {
		$this->connect();
		$this->selectDb();
		$this->rootPath = $_SERVER['DOCUMENT_ROOT'];
	}

	public function connect() {
		mysql_pconnect($this->hostName, $this->userName, $this->passwrd) or die("Incomplete connect");
		mysql_query("SET NAMES UTF8");
	}

	public function selectDb(){
		mysql_select_db($this->dbname);
	}

	public function fetchDataList($sqlQuery) {
		$arrData = array();
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			exit;
		}
		if (mysql_num_rows($result) > 0) {
			while ($row = mysql_fetch_assoc($result)) {
				$arrData[] = $row;
			}
		}
		mysql_free_result($result);
		return $arrData;
	}

	public function fetchDataRow($sqlQuery) {
		$arrData = array();
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			exit;
		}
		if (mysql_num_rows($result) > 0) {
			while ($row = mysql_fetch_assoc($result)) {
				$arrData = $row;
			}
		}
		mysql_free_result($result);
		return $arrData;
	}

	public function commitQuery($sqlQuery) {
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			exit();
		} else {
			return "Y";
		}
	}

	public function queryLimit($page) {
		if (isset($page) && trim($page) != "") {
			$this->startLimit = ($page - 1) * $this->perPage;
		}
		$queryLimit = " ORDER BY id ASC LIMIT " . $this->startLimit . ", " . $this->perPage . "";
		return $queryLimit;
	}

		public function getMaxPageCustomer($tableName) {
		$sqlQuery = "SELECT id FROM " .  $tableName ." WHERE user_type='g'";
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			exit;
		}
		$numRow = mysql_num_rows($result);
		$maxPageCustomer = ceil($numRow / $this->perPage);
		return $maxPageCustomer;
	}
		public function buildPageBtnCustome($page, $maxPageCustomer) {
		$pageBtnCustomer = "<table width=\"100%\">\n";
		$pageBtnCustomer .= "<tr>\n";
		$pageBtnCustomer .= "<td width=\"10%\"><input type=\"button\" value=\" << \" onclick=\"location = '?page=1';\"></td>\n";
		if ($page > 1) {
			$pageBtnCustomer .= "<td width=\"40%\"><input type=\"button\" value=\" < \" onclick=\"location = '?page=" . ($page - 1) . "';\"></td>\n";
		} else {
			$pageBtnCustomer .= "<td width=\"40%\"><input type=\"button\" value=\" < \" onclick=\"location = '?page=1';\"></td>\n";
		}
		if ($page == $maxPageCustomer) {
			$pageBtnCustomer .= "<td width=\"40%\" align=\"right\"><input type=\"button\" value=\" > \" onclick=\"location = '?page=" . $maxPageCustomer . "';\"></td>\n";
		} else {
			$pageBtnCustomer .= "<td width=\"40%\" align=\"right\"><input type=\"button\" value=\" > \" onclick=\"location = '?page=" . ($page + 1) . "';\"></td>\n";
		}
		$pageBtnCustomer .= "<td width=\"10%\" align=\"right\"><input type=\"button\" value=\" >> \" onclick=\"location = '?page=" . $maxPageCustomer . "';\"></td>\n";
		$pageBtnCustomer .= "</tr>\n";
		$pageBtnCustomer .= "</table>\n";
		return $pageBtnCustomer;
	}

		public function getMaxPageEmployee($tableName) {
		$sqlQuery = "SELECT id FROM " .  $tableName ." WHERE user_type!='g' AND user_type!='a'";
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			exit;
		}
		$numRow = mysql_num_rows($result);
		$maxPageEmployee = ceil($numRow / $this->perPage);
		return $maxPageEmployee;
	}

	public function buildPageBtnEmployee($page, $maxPageEmployee) {
		$pageBtnEmployee = "<table width=\"100%\">\n";
		$pageBtnEmployee .= "<tr>\n";
		$pageBtnEmployee .= "<td width=\"10%\"><input type=\"button\" value=\" << \" onclick=\"location = '?page=1';\"></td>\n";
		if ($page > 1) {
			$pageBtnEmployee .= "<td width=\"40%\"><input type=\"button\" value=\" < \" onclick=\"location = '?page=" . ($page - 1) . "';\"></td>\n";
		} else {
			$pageBtnEmployee .= "<td width=\"40%\"><input type=\"button\" value=\" < \" onclick=\"location = '?page=1';\"></td>\n";
		}
		if ($page == $maxPageEmployee) {
			$pageBtnEmployee .= "<td width=\"40%\" align=\"right\"><input type=\"button\" value=\" > \" onclick=\"location = '?page=" . $maxPageEmployee . "';\"></td>\n";
		} else {
			$pageBtnEmployee .= "<td width=\"40%\" align=\"right\"><input type=\"button\" value=\" > \" onclick=\"location = '?page=" . ($page + 1) . "';\"></td>\n";
		}
		$pageBtnEmployee .= "<td width=\"10%\" align=\"right\"><input type=\"button\" value=\" >> \" onclick=\"location = '?page=" . $maxPageEmployee . "';\"></td>\n";
		$pageBtnEmployee .= "</tr>\n";
		$pageBtnEmployee .= "</table>\n";
		return $pageBtnEmployee;
	}


	public function getMaxPage($tableName) {
		$sqlQuery = "SELECT id FROM " .  $tableName;
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			exit;
		}
		$numRow = mysql_num_rows($result);
		$maxPage = ceil($numRow / $this->perPage);
		return $maxPage;
	}

	public function buildPageBtn($page, $maxPage) {
		$pageBtn = "<table width=\"100%\">\n";
		$pageBtn .= "<tr>\n";
		$pageBtn .= "<td width=\"10%\"><input type=\"button\" value=\" << \" onclick=\"location = '?page=1';\"></td>\n";
		if ($page > 1) {
			$pageBtn .= "<td width=\"40%\"><input type=\"button\" value=\" < \" onclick=\"location = '?page=" . ($page - 1) . "';\"></td>\n";
		} else {
			$pageBtn .= "<td width=\"40%\"><input type=\"button\" value=\" < \" onclick=\"location = '?page=1';\"></td>\n";
		}
		if ($page == $maxPage) {
			$pageBtn .= "<td width=\"40%\" align=\"right\"><input type=\"button\" value=\" > \" onclick=\"location = '?page=" . $maxPage . "';\"></td>\n";
		} else {
			$pageBtn .= "<td width=\"40%\" align=\"right\"><input type=\"button\" value=\" > \" onclick=\"location = '?page=" . ($page + 1) . "';\"></td>\n";
		}
		$pageBtn .= "<td width=\"10%\" align=\"right\"><input type=\"button\" value=\" >> \" onclick=\"location = '?page=" . $maxPage . "';\"></td>\n";
		$pageBtn .= "</tr>\n";
		$pageBtn .= "</table>\n";
		return $pageBtn;
	}
	
}
?>